package com.pet.serve.service;

import com.pet.serve.entity.Invest;
import com.pet.serve.repository.InvestRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class InvestService {

    private final InvestRepository investRepository;

    @Autowired
    public InvestService(InvestRepository investRepository) {
        this.investRepository = investRepository;
    }

    // 根据用户名获取充值记录
    public List<Invest> getInvestsByUsername(String username) {
        return investRepository.findByUsername(username);
    }

    // 根据状态获取充值记录
    public List<Invest> getInvestsByStatus(Invest.InvestStatus status) {
        return investRepository.findByStatus(status);
    }

    // 根据时间范围获取充值记录
    public List<Invest> getInvestsByTimeRange(Date startDate, Date endDate) {
        return investRepository.findByTimeBetween(startDate, endDate);
    }

    // 获取所有充值记录
    public List<Invest> getAllInvests() {
        return investRepository.findAllInvests();
    }

    // 获取待审批的充值记录
    public List<Invest> getPendingInvests() {
        return investRepository.findPendingInvestments();
    }

    // 根据审批账号获取充值记录
    public List<Invest> getInvestsByApproval(String approval) {
        return investRepository.findByApproval(approval);
    }

    // 根据状态获取充值记录
    public List<Invest> getInvestsByStatus(String status) {
        return investRepository.findByStatus(status);
    }

    // 批量更新充值记录的审批状态
    @Transactional
    public void updateInvestStatus(List<Long> ids, String status, String approval) {
        investRepository.updateStatusByIds(status, approval, ids);
    }

    public void addInvest(Invest invest) {
        investRepository.save(invest);
    }

    public Page<Invest> findAllInvestsWithPagination(int pageNo, int pageSize, String username, Date starttime, Date endtime) {
        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
        // 根据条件查询充值记录，可以根据需要添加用户名和时间范围的过滤
        return investRepository.findAllByConditions(username, starttime, endtime, pageable);
    }

    public Page<Invest> findInvestsByStatusWithPagination(Invest.InvestStatus status, int pageNo, int pageSize, String username, Date starttime, Date endtime) {
        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
        return investRepository.findByStatus(status, username, starttime, endtime, pageable);
    }

    public Page<Invest> findAllInvestsWithPagination(int pageNo, int pageSize, String username, String approval, Date starttime, Date endtime) {
        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
        return investRepository.findAllByFilters(username, approval, starttime, endtime, pageable);
    }

    public Page<Invest> findInvestsByStatusWithPagination(Invest.InvestStatus status, int pageNo, int pageSize, String username, String approval, Date starttime, Date endtime) {
        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
        return investRepository.findByStatusAndFilters(status, username, approval, starttime, endtime, pageable);
    }


    // 批准充值申请
    public boolean approveInvestment(Long investId, String approval, BigDecimal money) {
        Invest invest = investRepository.findById(investId).orElse(null);
        if (invest == null) {
            return false;
        }
        invest.setStatus(Invest.InvestStatus.yes);
        invest.setApproval(approval); // 设置审批信息
        investRepository.save(invest);
        return true;
    }

    // 拒绝充值申请
    public boolean refuseInvestment(Long investId, String approval) {
        Invest invest = investRepository.findById(investId).orElse(null);
        if (invest == null) {
            return false;
        }
        invest.setStatus(Invest.InvestStatus.refused);
        invest.setApproval(approval); // 设置审批信息
        investRepository.save(invest);
        return true;
    }

}
